package com.gao.security.autoconfig.verificationcode.handler;


import com.gao.security.verificationcode.common.BaseVo;
import com.gao.security.verificationcode.common.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2018-12-18 21:46
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler  {
    /* 处理400类异常 */

    @ExceptionHandler(value =BadCredentialsException.class)
    protected BaseVo handleBadCredentialsException(BadCredentialsException e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return BaseVo.failure(ResponseCode.USER_LOGIN_ERROR, e.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    protected BaseVo handleBadCredentialsException2(ConstraintViolationException e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return BaseVo.failure(ResponseCode.USER_LOGIN_ERROR, e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    protected BaseVo handleBadCredentialsException3(Exception e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return BaseVo.failure(ResponseCode.INTERFACE_ADDRESS_INVALID, e.getMessage());
    }
}
